package com.lyn.tencent_health_code.utils;

import com.alibaba.fastjson.JSONObject;
import com.lyn.tencent_health_code.entity.LoginUser;
import com.lyn.tencent_health_code.service.EncrypDESService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

/**
 * @author almost
 */
public class TokenUtils {
    private static Logger logger= LoggerFactory.getLogger(TokenUtils.class);
    /**
     * tokeName
     */
    public static final String TOKEN_NAME = "health_code_token";
    /**
     * 过期时间
     */
    public static final int EXPIRE_DATE = 60 * 60 * 24;

    /**
     * 创建token
     *
     * @param encoder
     * @param loginUser
     * @return
     */
    public static String createToken(EncrypDESService encoder, LoginUser loginUser) {
        return encoder.encrypt(loginUser.toString().concat("&").concat(UUID.randomUUID().toString().replace("-", "")));
    }

    /**
     * 根据token获取用户信息
     * @param token
     * @return
     */
    public static LoginUser getLoginUser(EncrypDESService encoder, String token) throws Exception{
        if (TextUtils.isEmpty(token)) {
            return null;
        }
        String[] split = encoder.decrypt(token).split("&");
        if (split==null||split.length<2){
            return null;
        }
        LoginUser loginUser = JSONObject.toJavaObject(JSONObject.parseObject(split[0]), LoginUser.class);
        return loginUser;
    }


    /**
     * 获取token
     *
     * @param request
     * @return
     */
    public static String getToken(HttpServletRequest request) {
        if (request == null) {
            return null;
        }
        String token = request.getHeader(TOKEN_NAME);
        if (TextUtils.isEmpty(token)) {
            Cookie[] cookies = request.getCookies();
            if (cookies==null){
                return null;
            }
            for (Cookie c : cookies) {
                if (c==null){
                    continue;
                }
                if (c.getName().equals(TOKEN_NAME)) {
                    return c.getValue();
                }
            }
        }else {
            return token;
        }
        return null;
    }

    /**
     * 设置token
     *
     * @param token
     * @param response
     * @return
     */
    public static boolean setToken(String token, HttpServletResponse response) {
        if (response == null) {
            logger.info("response == null");
            return false;
        }
        if (TextUtils.isEmpty(token)) {
            logger.info("TextUtils.isEmpty(token)");
            return false;
        }
        Cookie cookie = new Cookie(TOKEN_NAME, token);
        cookie.setMaxAge(EXPIRE_DATE);
        response.addCookie(cookie);
        return true;
    }

    /**
     * 删除浏览器token
     *
     * @param response
     */
    public static void deleteToken(RedisUtil redisUtil, HttpServletRequest request, HttpServletResponse response) {
        String token = getToken(request);
        redisUtil.del(token);
        Cookie cookie = new Cookie(TOKEN_NAME, null);
        cookie.setMaxAge(0);
        response.addCookie(cookie);

        logger.info("deleteToken");
    }
}
